/*---------------------------------------
* Author:ZJ
* createDate:Tuesday, August 07, 2012
* Description:
*
------------------------------------------*/

#ifndef SERVSPACE_CONNECTION_HPP
#define SERVSPACE_CONNECTION_HPP

#include "StdAfx.h"
#include "handler\handler.h"

namespace fly {

	namespace jld{

		namespace server {	

			class connection_manager;
			class connection;
			typedef boost::shared_ptr<connection> connection_ptr;
			//single connection
			class connection:public boost::enable_shared_from_this<connection>,
				private boost::noncopyable
			{
			public:
				explicit connection(boost::asio::io_service& io_service,
					connection_manager& manager, handler* handler);

				~connection();

				/// Get the socket associated with the connection.
				boost::asio::ip::tcp::socket& socket();

				conn_info& info();

				/// Start the first asynchronous operation for the connection.
				void start();

				/// Stop all asynchronous operations associated with the connection.
				void stop();

				//send msg to cnn.
				void asyn_write(const_buf_ptr send_buf);

			private:

				void read_header();

				void read_body();

				void handle_header_read(const boost::system::error_code& e,
					std::size_t bytes_transferred);

				void handle_body_read(const boost::system::error_code& e,
					std::size_t bytes_transferred);

				/// Handle completion of a write operation.
				void handle_write(const boost::system::error_code& e, const_buf_ptr const_send_buf);

				/// Socket for the connection.
				asio::ip::tcp::socket socket_;

				/// The manager for this connection.
				connection_manager& connection_manager_;

				handler* handler_;

				recv_buf recv_buf_;

				send_buf send_buf_;

				conn_info conn_info_;
			};

		}
	}
}


#endif // SERVSPACE_CONNECTION_HPP